﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CivSharp.Common;
using EmezenWarchief.Enums;

namespace EmezenWarchief
{
    class TerekStrategy : IEmezenStrategy
    {
        private int turn;
        private WorldInfo world;
        private List<UnitInfo> myUnits;
        private List<CityInfo> myCities;

        private PlayerInfo myPlayer;

        private List<EPlayer> enemyPlayers;

        List<String> researches;

        public string PlayerName
        {
            get
            {
                return "farm";
            }
        }

        public void RefreshWorldInfo(int turn, WorldInfo world)
        {
            this.turn = turn;
            this.world = world;

            // kikeresem a saját játékosomat is a világ leírásából 
            this.myPlayer = this.world.Players.Where(player => player.Name == Constants.NAME_PREFIX + this.PlayerName).Single();

            this.researches = new List<string>();
            this.researches.AddRange( this.myPlayer.Researched );

            this.myUnits = new List<UnitInfo>();
            this.myUnits.AddRange(this.world.Units.Where(unit => unit.Owner == this.myPlayer.Name));

            // kikeresem a városaimat 

            this.myCities = new List<CityInfo>();
            this.myCities.AddRange(this.world.Cities.Where(city => city.Owner == this.myPlayer.Name));

            this.enemyPlayers = new List<EPlayer>();
            foreach(PlayerInfo player in this.world.Players){
                if (player.Name != myPlayer.Name)
                {
                    List<CityInfo> eCities = new List<CityInfo>();
                    eCities.AddRange(this.world.Cities.Where(city => city.Owner == player.Name));

                    List<UnitInfo> eUnits = new List<UnitInfo>();
                    eUnits.AddRange(this.world.Units.Where(unit => unit.Owner == player.Name));

                    this.enemyPlayers.Add( new EPlayer(player, eUnits, eCities ) );
                }
            }
        }


        public void ActionResult(bool succeeded)
        {
            
        }

        public void CityLost(int positionX, int positionY)
        {

        }

        public void EnemyDestroyed(string playerName)
        {

        }

        public void GameOver(bool winner, string message)
        {

        }

        public CivSharp.Common.BuildingData OnBuilding()
        {

            if ( ReturnCalculator.calculateReturn(this.myCities.Count, Research.írás) <= 7 && !this.researches.Contains( Research.írás.ToString() ) )
            {
                return null;
            }


            if ( ReturnCalculator.calculateReturn(this.myCities.Count, Research.bíróság) <= 7 && !this.researches.Contains(Research.bíróság.ToString()))
            {
                return null;
            }


            if( this.myPlayer.Money >= Constants.CITY_PRICE && (Constants.MAX_TURNS - turn > 7 )  ){

                foreach(UnitInfo unit in myUnits){
                
                        if(  !myCities.Exists ( city => (city.PositionX == unit.PositionX && city.PositionY == unit.PositionY) ) ){
                            BuildingData buildingData = new BuildingData();
                            buildingData.PositionX = unit.PositionX;
                            buildingData.PositionY = unit.PositionY;
                            return buildingData;
                        }
                }

            }

            return null;
        }

        public CivSharp.Common.MovementData OnMovement()
        {
            foreach (UnitInfo unit in myUnits)
            {
                if (unit.MovementPoints > 0 )
                {
                    if(  myCities.Exists ( city => (city.PositionX == unit.PositionX && city.PositionY == unit.PositionY) ) ){
                        MovementData movementData = new MovementData();
                        movementData.FromX = unit.PositionX;
                        movementData.FromY = unit.PositionY;

                        movementData.ToX = unit.PositionX + 1;
                        movementData.ToY = unit.PositionY ; 

                        movementData.UnitID = unit.UnitID;

                        unit.PositionX = movementData.ToX;
                        unit.PositionY = movementData.ToY;


                        return movementData;
                    }
                }
            }

            return null;

        }

        public CivSharp.Common.ResearchData OnResearch()
        {

            if (this.myPlayer.Money >= Constants.WRITING_PRICE && ReturnCalculator.calculateReturn(this.myCities.Count, Research.írás) <= 7 && !this.researches.Contains( Research.írás.ToString() ) )
            {
                ResearchData rData = new ResearchData();
                rData.WhatToResearch = Research.írás.ToString();

                return rData;
            }


            if (this.myPlayer.Money >= Constants.COURTHOUSE_PRICE && ReturnCalculator.calculateReturn(this.myCities.Count, Research.bíróság) <= 7 && !this.researches.Contains(Research.bíróság.ToString()))
            {
                ResearchData rData = new ResearchData();
                rData.WhatToResearch = Research.írás.ToString();

                return rData;
            }


            return null;
        }

        public CivSharp.Common.TrainingData OnTraining()
        {
            if (this.myUnits.Count < 1)
            {
                TrainingData trainingData = new TrainingData();

                CityInfo city = myCities[0];

                trainingData.UnitTypeName = UnitType.talpas.ToString();
                trainingData.PositionX = city.PositionX;
                trainingData.PositionY = city.PositionY;

                myUnits.Add(new UnitInfo());

                return trainingData;
            }

            return null;
        }

        public void UnitLost(string unitID)
        {
        }

    }
}
